import axios from 'axios'
import Vue from 'vue'
import qs from 'qs'
import ElementUI, { Loading } from 'element-ui'
import * as Login from './login'
Vue.use(ElementUI)
var vue = new Vue()
class HttpReq {

    constructor() {
        this.LoadingFlag = 0
        this.LoadingText = '努力加载中~'
    }
    /**
   * 请求加载滚动条
   */
    _RequestLoading() {
        let self = this
        this.LoadingFlag = Loading.service({ 'text': self.LoadingText })
    }
    /**
   * 关闭滚动条
   */
    _CloseLoading() {
        this.LoadingFlag.close()
    }
    /**
   * 请求拦截器(发送前)
   */
    _RequestBefore() {
        let self = this
        axios.interceptors.request.use(function (config) {
            self._RequestLoading()
            return config
        }, function (error) {
            return Promise.reject(error)
        })

    }
    /**
   * 请求拦截器(响应结果)
   */
    _RequestEnd() {
        let self = this
        axios.interceptors.response.use(function (response) {
            self._CloseLoading()
            return response
        }, function (error) {
            return Promise.reject(error)
        })
    }

    /**
   * 发送post请求(formdata格式)
   * @param {*} url 
   * @param {*} data 
   * @param {*} callback 
   */
    _POST(url, data = {}, callback = function () { }) {


    

        this._RequestBefore()
        this._RequestEnd()
        axios({
            method: 'post',
            data: qs.stringify(data),
            url: url,
            responseType: 'json',
            withCredentials: true,
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            }
        })
            .then(function (response) {
                let data = response.data
                if (data.code != 200) {
                    vue.$message.error(data.msg)
                    if (data.code == 204) {
                        // setTimeout(() => {
                        //     window.location.href="/#/login"
                        // }, 2000)
                    }
                    
                    if (data.code == 205) {
                        setTimeout(() => {
                            window.location.href="/#/home"
                        }, 2000)
                    }


                }
                else {
                    callback(data)
                }
            }).catch(function (error) {
                //console.log(error)
                vue.$message.error('网络出错了,请刷新重试~')
                self._CloseLoading()
            })
    }


/**
 * post json data
 * @param {*} url 
 * @param {*} data 
 * @param {*} callback 
 */
    _REQUEST_JSON(url, data = {}, callback = function () { },type='post') {
        
           
                this._RequestBefore()
                this._RequestEnd()
                if(type=='get'){
                    url+="?"+qs.stringify(data)
                }
                axios({
                    method: type,
                    data:data,
                    url: url,
                    responseType: 'json',
                    withCredentials: true,
                    headers: {
                        'Content-Type': 'application/json',
                        'Token':Login.getLogin().token
                    }
                })
                    .then(function (response) {
                        let data = response.data
                        if (data.code != 200) {
                            vue.$message.error(data.msg)
                            if (data.code == 204) {
                                // setTimeout(() => {
                                //     window.location.href="/#/login"
                                // }, 2000)
                            }
                            
                            if (data.code == 205) {
                                setTimeout(() => {
                                    window.location.href="/#/home"
                                }, 2000)
                            }
        
        
                        }
                        else {
                            callback(data)
                        }
                    }).catch(function (error) {
                        //console.log(error)
                        vue.$message.error('网络出错了,请刷新重试~')
                        self._CloseLoading()
                    })
            }
        


    /**
     * 
     * @param 上传文件到服务器 url 
     * @param {*} data 
     * @param {*} callback 
     */

    _POST_FILE(url, data = {}, callback = function () { }) {

        this._RequestBefore()
        this._RequestEnd()
        axios({
            method: 'post',
            data: data,
            url: url,
            responseType: 'json',
            withCredentials: true,
            headers: {
                'Content-Type': 'multipart/form-data'
            }
        })
            .then(function (response) {
                let data = response.data
                if (data.code != 200) {
                    vue.$message.error(data.msg)
                }
                else {
                    callback(data)
                }
            }).catch(function (error) {
                //console.log(error)
                vue.$message.error('网络出错了~')
            })
    }

    _DOWNLOAD_FILE(url) {

        let iframe = document.createElement('iframe')
        iframe.style.display = 'none'
        iframe.src = url
        iframe.onload = function () {
            document.body.removeChild(iframe)
        }
        document.body.appendChild(iframe)
    }


    _GET(url, callback) {

    }
}
export { HttpReq }